package com.lyn.demo;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.upload.UploadFile;
//import com.lyn.io.FileKit;

import java.io.File;
import java.util.List;

/**
 * @author lynmonade
 * @create 2019-02-06 15:22
 */
public class FileController extends Controller {

    /**
     * 文件下载
     */
    public void download() {
        //第一个参数是原始文件名，第二个参数是下载文件的新名称
        String fileName = getPara("fileName");
        renderFile(fileName, "renameDownloadFile1.sql");
    }

    /**
     * 单个文件上传
     */
    public void upload() {
        UploadFile uploadFile = getFile(); //调用getFile方法时，就直接把文件落地到BaseUploadPath
        String fileName = uploadFile.getFileName();
        String originalFileName = uploadFile.getOriginalFileName();

        //后续你还可以把文件复制到其他目录
        String newName = getPara("newName");
        File srcFile = uploadFile.getFile();
        String destFile = JFinal.me().getConstants().getBaseUploadPath()+File.separator+"path"+File.separator+newName;
        try {
            //FileKit.copyFile(srcFile, new File(destFile));
            renderText("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("上传失败");
        }
    }

    /**
     * 多个文件上传
     */
    public void multiUpload() {
        List<UploadFile> list = getFiles(); //调用getFiles()时，jfinal自动把文件保存到BaseUploadPath
        for(UploadFile uploadFile : list) {
            System.out.println(uploadFile.getFileName());
        }
        System.out.println(getPara("para"));
        renderText("上传成功");
    }
}
